#!/usr/bin/env bash

#延时输出
function echo_sleep() {
  if [ "$2" ]; then
    sleep $2
  else
    sleep 1
  fi

  echo -e "$1"
}
#红色输出
function echo_red() {
  echo -e "\033[1;31m$1\033[0m"
}
#绿色输出
function echo_green() {
  echo -e "\033[1;32m$1\033[0m"
}
#黄色输出
function echo_yellow() {
  echo -e "\033[1;33m$1\033[0m"
}
#蓝色输出
function echo_blue() {
  echo -e "\033[36m$1\033[0m"
}
#菜单
function echo_menu() {
  echo -e "\033[36m$1\033[0m $2"
}
#分隔线
function echo_line() {
  echo -e "\033[36m=========================$1==========================\033[0m"
}
function echo_done() {
  sleep 0.5
  echo "$(gettext 'complete')"
}
#输出√
function echo_check() {
  echo -e "$1 \t [\033[32m √ \033[0m]"
}

function echo_warn() {
  echo -e "[\033[33m WARNING \033[0m] $1"
}

function echo_failed() {
  echo_red "fail"
}

function log_success() {
  echo_green "[SUCCESS] $1"
}

function log_warn() {
  echo_yellow "[WARN] $1"
}

function log_error() {
  echo_red "[ERROR] $1"
}
#判断当前系统并安装指定命令
function install_soft() {
  if command -v dnf >/dev/null; then
    dnf -q -y install "$1"
  elif command -v yum >/dev/null; then
    yum -q -y install "$1"
  elif command -v apt >/dev/null; then
    apt-get -qqy install "$1"
  elif command -v zypper >/dev/null; then
    zypper -q -n install "$1"
  elif command -v apk >/dev/null; then
    apk add -q "$1"
    command -v gettext >/dev/null || {
      apk add -q gettext-dev python3
    }
  else
    echo -e "[\033[31m ERROR \033[0m] $1 command not found, Please install it first"
    exit 1
  fi
}

# 判断命令是否存在并安装
function prepare_install() {
  for i in "$@"; do
    command -v $i &>/dev/null || install_soft $i
  done
}

# 封装read命令
# $! 提示信息
# $2 需要赋值的变量
function read_input() {
  local msg=$1
  local param=$2

  read -p "${msg}" input

  if [ "$input" != "" ]; then
    param=$input
  fi
  echo "${param}"
}

function is_confirm() {
  read -r confirmed
  if [[ "${confirmed}" == "y" || "${confirmed}" == "Y" || ${confirmed} == "" ]]; then
    return 0
  else
    return 1
  fi
}

# 修改配置文件(找不到key就新增一条)
# 例:config_add_or_update "daemonize1 yes" /opt/redis/redis.conf
function config_add_or_update() {
  local keywords=$(echo $1 | awk -F "[= ]+" '{print $1}')
  local SearchResult=$(grep "^${keywords}" "$2")
  if [ -z "${SearchResult}" ]; then #空为真，非空为假
    echo $1 >>$2
  else
    sed -i "s/^${keywords}.*/$1/" $2
  fi
}

function echo_logo() {
  cat <<"EOF"
 _       _   __   _   _   _  __    __       _____   _____   _____   _       _____
| |     | | |  \ | | | | | | \ \  / /      |_   _| /  _  \ /  _  \ | |     /  ___/
| |     | | |   \| | | | | |  \ \/ /         | |   | | | | | | | | | |     | |___
| |     | | | |\   | | | | |   }  {          | |   | | | | | | | | | |     \___  \
| |___  | | | | \  | | |_| |  / /\ \         | |   | |_| | | |_| | | |___   ___| |
|_____| |_| |_|  \_| \_____/ /_/  \_\        |_|   \_____/ \_____/ |_____| /_____/
EOF
}

# 判断数组是否包含某个元素
# $(contains "${user_array[@]" "$user_name")
function contains() {
  local n=$#
  local value=${!n}
  for ((i = 1; i < $#; i++)); do
    if [ "${!i}" == "${value}" ]; then
      echo "y"
      retrun 0
    fi
  done
  echo "n"
  return 1
}

break_end() {
  echo_red "操作完成"
  echo "按任意键继续..."
  read -n 1 -s -r -p ""
  echo ""
  clear
}

# 更新脚本
function update_sh() {
  echo -e "\033[1;31m 下载相关sh脚本至${LINUX_TOOLS_DIR} - download install script to ${LINUX_TOOLS_DIR} \033[0m"

  cd /opt || exit 1

  timeout 10 wget -qO linuxTools.tar.gz ${DOWNLOAD_URL} || {
    rm -f /opt/linuxTools.tar.gz
    echo -e "[\033[31m ERROR \033[0m] 下载失败 - Failed to download"
    exit 1
  }

  tar -xf /opt/linuxTools.tar.gz -C /opt/linuxTools || {
    rm -rf /opt/linuxTools
    echo -e "[\033[31m ERROR \033[0m] 解压失败 - Failed to unzip"
    exit 1
  }
  rm -f /opt/linuxTools.tar.gz
}

# 下载文件
function DOWNLOAD_FILE() {
  echo -e "\033[1;31m 下载地址:${DOWNLOAD_URL_FILE} \033[0m"

  cd /tmp || exit 1

  timeout 10 wget -qO $1 ${DOWNLOAD_URL_FILE}/$1 || {
    rm -f /tmp/$1
    echo -e "[\033[31m ERROR \033[0m] 下载失败 - Failed to download"
    exit 1
  }

}